<?php
require_once '../sub/init.php';
header("Cache-control: private");
require_once 'chkUadm.php';
if(!in_array('u_top',$QXARR))exit(noauth());
require_once ZEAI.'cache/config_vip.php';
$urole = json_decode($_ZEAI['urole'],true);
sort($urole);
if ($submitok == 'ajax_addupdate' || $submitok == 'ajax_modupdate'){
	$day=intval($day);
	if(str_len($endC)>1000)json_exit(array('flag'=>0,'msg'=>'【过期后通知内容】请小于1000字节'));
	//费用
	foreach($urole as $RV){
		$price1 = 'price1_'.$RV['g'];$price1 = abs(floatval($$price1));
		$price2 = 'price2_'.$RV['g'];$price2 = abs(floatval($$price2));
		//if(empty($price1) || empty($price2))json_exit(array('flag'=>0,'msg'=>'亲，每项请填写大于0的金额'));
		$json_price['1_'.$RV['g']] = $price1;
		$json_price['2_'.$RV['g']] = $price2;
	}
	$json_price = encode_json($json_price);
	$endC = dataIO($endC,'in',1000);
}
switch ($submitok){
	case "ajax_addupdate":
		$db->query("INSERT INTO ".__TBL_TOP__." (day,json_price,endC) VALUES ('$day','$json_price','$endC')");
		$fid=$db->insert_id();
		AddLog('新增【置顶项目id：'.$fid.'】->'.$day.'天');
		json_exit(array('flag'=>1,'msg'=>'新增成功'));
	break;
	case "ajax_modupdate":
		if(!ifint($id))json_exit(array('flag'=>0,'msg'=>'id:forbidden'));
		$db->query("UPDATE ".__TBL_TOP__." SET day='$day',json_price='$json_price',endC='$endC' WHERE id=".$id);
		AddLog('修改【置顶项目id：'.$id.'】');
		json_exit(array('flag'=>1,'msg'=>'修改成功'));
	break;
	case "ajax_u_top_flag":
		if(!ifint($id))json_exit(array('flag'=>0,'msg'=>'forbidden'));
		$flag=intval($flag);
		$db->query("UPDATE ".__TBL_TOP__." SET flag=$flag WHERE id=".$id);
		$flag=($flag==1)?'显示':'隐藏';
		AddLog('修改【置顶项目id：'.$id.'】状态->'.$flag);
		json_exit(array('flag'=>1,'msg'=>$flag.'成功'));
	break;
	case "ajax_delupdate":
		if(!ifint($id))json_exit(array('flag'=>0,'msg'=>'forbidden'));
		$db->query("DELETE FROM ".__TBL_TOP__." WHERE id=".$id);
		$db->query("DELETE FROM ".__TBL_TOP_U__." WHERE fid=".$id);
		AddLog('删除【置顶项目id：'.$id.'】');
		json_exit(array('flag'=>1,'msg'=>'删除成功'));
	break;
	case"ding":
		if(!ifint($id))json_exit(array('flag'=>0,'msg'=>'forbidden'));
		$db->query("UPDATE ".__TBL_TOP__." SET px=".ADDTIME." WHERE id=".$id);
		AddLog('置顶【置顶项目id：'.$id.'】');
		json_exit(array('flag'=>1,'msg'=>'置顶成功'));
	break;
}
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<style>
.table0 tr td{font-size:12px;padding:5px 0;text-align:left;border:0;color:#999}
.table0 tr {border:0}
.table0 tr td b{color:#f00;font-weight:normal}
.box{min-width:1000px}
</style>
</head>
<body>
<div class="box">
	<div class="boxL"><a href="u_top.php" class="ed">付费置顶管理</a></div>
  <div class="boxR">
        <!---->
        <?php if ($submitok == "add" || $submitok == "mod") {
            if($submitok == "mod"){
                $row = $db->ROW(__TBL_TOP__,"*","id=".$id,"name");
                if ($row){
                    $id      = $row['id'];
                    $day     = $row['day'];
                    $endC    = dataIO($row['endC'],'wx');
                    $addtime = $row['addtime'];
                    $flag    = $row['flag'];
                    $json_price = $row['json_price'];
                }else{exit('forbidden');}
            }?>
            <form id="ZEAIFORM" name="ZEAIFORM" method="post">
            <table class="table W98_ size2">
            <tr><td height="50" colspan="2" align="center" class="S18 B"><?php echo ($submitok == 'add')?'新增':'修改';?>手机端置顶收费项目</td></tr>
            <tr>
            <td class="tdL">置顶有效期</td>
            <td class="tdR"><input name="day" type="text" class="W80 size2" maxlength="3" value="<?php echo $day;?>"> 天　　　　<span class="tips S12">最少1天，推荐7天，15天，30天，60天，90天，180天</span></td>
            </tr>      
            <!--报名费用-->
            <tr><td class="tdL">置顶价格</td>
            <td class="tdR">
                <?php
                $json_price = json_decode($json_price,true);
                if (is_array($urole) && count($urole)>0){?>
                <table class="table0" style="margin:0">
                    <?php
                    foreach($urole as $RV){
                        $grade = $RV['g'];?>
                        <tr>
                        <td align="left"> <font style="vertical-align:middle" class="B"><?php echo $RV['t'];?></font> <i class="ico Caaa S18" style="margin:0 5px;vertical-align:middle">&#xe62d;</i> </td>
                        <td width="300" align="left">
                         <?php echo uicon_grade_all($grade,1,1); ?>男 <input name="price1_<?php echo $grade;?>" id="price1_<?php echo $grade;?>" type="text" class="size2 W50 FVerdana" maxlength="6" value="<?php echo floatval($json_price['1_'.$grade]);?>"> 元　
                         <?php echo uicon_grade_all($grade,1,2); ?>女 <input name="price2_<?php echo $grade;?>" id="price2_<?php echo $grade;?>" type="text" class="size2 W50 FVerdana" maxlength="6" value="<?php echo floatval($json_price['2_'.$grade]);?>"> 元
                        </td>
                        </tr>
                        <?php
                    }?>
                </table>
                <?php }else{
                    echo "<div class='nodatatipsS'>暂无用户组<br><br><a class='btn size1' href='urole.php'>新增用户组</a></div>";
                }
                ?>
                <font class="tips2">每项请填大于0的金额，用户将支付费用后置顶，如果有相关套餐送置顶一次，可以给其增加账户余额，用户使用余额来支付</font>
            </td></tr>
              <tr>
              <td height="10" valign="top" class="tdL">过期后通知内容</td>
              <td valign="top" class="tdR lineH150"><textarea name="endC" id="endC" placeholder="如果为空，将使用默认内容发送" rows="4" class="W100_ S14"><?php echo $endC;?></textarea></td>
              </tr>
              <tr>
                <td height="10" valign="top" class="tdL">&nbsp;</td>
                <td valign="top" class="tdR"><button type="button" class="btn size3" id="save">确定并保存</button>
</td>
              </tr>
              </table>
                <?php if ($submitok == "add") {?>
                    <input name="submitok" id="submitok" type="hidden" value="ajax_addupdate">
                <?php }elseif($submitok == "mod"){ ?>
                    <input name="submitok" id="submitok" type="hidden" value="ajax_modupdate">
                    <input name="id" type="hidden" value="<?php echo $id;?>">
                <?php }?>
            </form>
        <!--MOD-->
        <?php }else{?>
            <!--LIST-->
            <div class="topsearch"><button type="button" class="btn size2" onClick="zeai.openurl('<?php echo SELF;?>?submitok=add')"><i class="ico add">&#xe622;</i> 新增置顶</button></div>    
          <?php
            $rt = $db->query("SELECT * FROM ".__TBL_TOP__." ORDER BY px DESC,id DESC");
            $total = $db->num_rows($rt);
            if ($total <= 0 ) {
				if ($submitok !== "add")echo "<div class='nodataico'><i></i>暂无内容<br><a class='btn size2' onClick=\"zeai.openurl('".SELF."?submitok=add')\">新增置顶</a></div>";
            } else {
                $page_skin = 2;$pagesize=20;require_once ZEAI.'sub/page.php';?>
              <table class="tablelist">
            	<tr>
                <th width="50" align="center">ID</th>
                <th width="50" align="center">置顶</th>
                <th width="120" align="center">有效期</th>
                <th width="350">　服务价格（元）</th>
                <th align="center">用户数量(点击操作：↓增删改↓)</th>
                <th>&nbsp;</th>
                <th width="100" align="center">状态</th>
                <th width="100" class="center">操作</th>
              </tr>
            <?php
            for($i=1;$i<=$pagesize;$i++) {
                $rows = $db->fetch_array($rt,'name');
                if(!$rows) break;
                $id = $rows['id'];
                $day = $rows['day'];
                $addtime = YmdHis($rows['addtime']);
                $json_price = json_decode($rows['json_price'],true);
                $flag = $rows['flag'];
                $unum = $db->COUNT(__TBL_TOP_U__,"fid=".$id);
            ?>
            <tr>
            <td width="50" height="60" align="center"><label for="id<?php echo $id; ?>"><?php echo $id;?></label></td>
            <td width="50" height="60" align="center"><a value="<?php echo $id;?>" class="topico" title="置顶"></a></td>
            <td width="120" align="center" class="S14"><?php echo $day;?>天</td>
            <td width="350" class="S14 C666">
                <?php
                if (is_array($urole) && count($urole)>0){?>
                <table class="table0" style="margin:10px 0">
                    <?php
                    sort($urole);
                    foreach($urole as $RV){
                        $grade = $RV['g'];
                        $sex1_price=floatval($json_price['1_'.$grade]);
                        $sex2_price=floatval($json_price['2_'.$grade]);
                        ?>
                        <tr>
                        <td align="left"><font style="vertical-align:middle"><?php echo $RV['t'];?></font> <i class="ico Caaa S18" style="margin:0 5px;vertical-align:middle">&#xe62d;</i> </td>
                        <td align="left">
                         <?php echo uicon_grade_all($grade,1,1); ?>男<?php if ($sex1_price >0){echo '<b>￥'.$sex1_price.'</b>';}else{echo '免费';}?>　
                         <?php echo uicon_grade_all($grade,1,2); ?>女<?php if ($sex2_price >0){echo '<b>￥'.$sex2_price.'</b>';}else{echo '免费';}?>
                        </td>
                        </tr>
                        <?php
                    }?>
                </table>
                <?php }else{
                    echo "<div class='nodatatipsS'>暂无用户组<br><br><a class='btn size1' href='urole.php'>新增用户组</a></div>";
                }
                ?>
              </td>
            <td align="center" class="S14"><a class="aBAI" onClick="zeai.iframe('置顶用户管理','u_top_u.php?fid=<?php echo $id;?>',700,550)"><?php echo ($unum>0)?'<font class="Cf00 FArial S14">'.$unum.'</font>人':0;?></a></td>
            <td>&nbsp;</td>
        <td width="100" align="center">
          <?php if ($grade>1){?>
          <input type="checkbox" id="flag<?php echo $id;?>" class="switch" value="<?php echo $flag;?>"<?php echo ($flag == 1)?' checked':'';?>><label value="<?php echo $id;?>" for="flag<?php echo $id;?>" class="switch-label"><i></i><b>显示</b><b>隐藏</b></label>
          <?php }?>
        </td>
            <td width="100" align="center" style="line-height:150%">
              <a value="<?php echo $id;?>" class="editbtn" title="修改"></a>
              <a value="<?php echo $id;?>" class="delbtn" title="删除" unum="<?php echo $unum;?>"></a>
              </td>
            </tr>
            <?php } ?>
            <?php if ($total > $pagesize){?>
            <tfoot><tr>
            <td colspan="8" class="Pleft10"><?php echo '<div class="pagebox FR">'.$pagelist.'</div>'; ?></td>
            </tr></tfoot>
            <?php } ?>
          </table>
          <?php } ?>
      <?php } ?>
      <!---->
	</div>
    <div class="clear"></div>
</div>
<?php if ($submitok == "add" || $submitok == "mod") {?>
	<script>
	save.onclick = function(){
		zeai.ajax({url:'u_top'+zeai.extname,form:ZEAIFORM},function(e){var rs=zeai.jsoneval(e);
			zeai.msg(rs.msg);
			if (rs.flag == 1){setTimeout(function(){zeai.openurl('u_top'+zeai.extname);},1000);}
		});
	}
	</script>
<?php }else{ ?>
<script>
	zeai.listEach('.switch-label',function(obj){
		obj.onclick = function(){
			var id = parseInt(obj.getAttribute("value"));
			var chkobj  = o('flag'+id);
			setTimeout(function(){
				var chkV = chkobj.checked;
				var flag = (chkobj.checked)?1:0;
				var postjson = {submitok:'ajax_u_top_flag',id:id,flag:flag};
				zeai.ajax({url:'u_top'+zeai.extname,data:postjson},function(e){var rs=zeai.jsoneval(e);
					zeai.msg(rs.msg);
					if (rs.flag == 1){setTimeout(function(){location.reload(true);},1000);}
				});
			},300);
		}
	});
	//topico
	zeai.listEach('.topico',function(obj){
		obj.onclick = function(){
			var id = parseInt(obj.getAttribute("value"));
			zeai.ajax({url:'u_top'+zeai.ajxext+'submitok=ding&id='+id},function(e){var rs=zeai.jsoneval(e);
				zeai.msg(rs.msg);if (rs.flag == 1){setTimeout(function(){location.reload(true);},1000);}
			});
		}
	});
	//mod
	zeai.listEach('.editbtn',function(obj){
		obj.onclick = function(){
			var id = parseInt(obj.getAttribute("value"));
			zeai.openurl('u_top'+zeai.ajxext+'submitok=mod&id='+id);
		}
	});
	//del
	zeai.listEach('.delbtn',function(obj){
		obj.onclick = function(){
			var id = parseInt(obj.getAttribute("value"));
			var unum = parseInt(obj.getAttribute("unum"));
			var title = obj.getAttribute("title");
			var tips = (unum>0)?'当前项目包含 '+unum+' 个用户正在置顶，删除后这些用户将自动清除置顶数据，下架不显示，':'';
			zeai.confirm('<b class="S18">请慎重！</b><br>'+tips+'确定要删除么？',function(){
				zeai.ajax('u_top'+zeai.ajxext+'submitok=ajax_delupdate&id='+id,function(e){var rs=zeai.jsoneval(e);
					zeai.msg(rs.msg);if (rs.flag == 1){setTimeout(function(){location.reload(true);},1000);}
				});
			});
		}
	});
</script>
<?php } ?>

<?php require_once 'bottomadm.php';?>